/**
 * 
 */
package mdb.com.vn.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import mdb.com.vn.model.Casa;
import mdb.com.vn.model.FixedDepositCasa;
import mdb.com.vn.model.FixedDeposits;
import mdb.com.vn.model.LuckyDraw;
import mdb.com.vn.services.MDBServices;
import mdb.com.vn.web.view.CSDataModel;
import mdb.com.vn.web.view.FDCSDataModel;
import mdb.com.vn.web.view.FDDataModel;

/**
 * @author tu.dod
 *
 */
@ManagedBean(name = "customerBeans")
@ViewScoped
public class CustomerBeans implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String customerId;
	private List<FixedDeposits> fixedDeposits;
	private List<FixedDepositCasa> fixedDepositCasas;
	private List<Casa> casas;
	
	private List<LuckyDraw> luckyDraws;
	private String luckyDrawCodes;
	private String luckyDrawCodesCS;
	private String luckyDrawCodesFDCS;
	
	private FixedDeposits depositsSelected;
	private Casa casaSelected;
	private FixedDepositCasa depositCasaSelected;
	
	private FDDataModel fdDataModel;
	private CSDataModel csDataModel;
	private FDCSDataModel fdcsDataModel;
	
	private int totalScradAvailable;
	private double totalAmountAvailable;
	
	
	@ManagedProperty("#{mdbServices}")
	private MDBServices mdbServices;

	@PostConstruct
	public void init() {
		customerId = "";
		luckyDrawCodes = "";
		fixedDeposits = new ArrayList<FixedDeposits>();
		fixedDepositCasas = new ArrayList<FixedDepositCasa>();
		casas = new ArrayList<Casa>();
		luckyDraws = new ArrayList<LuckyDraw>();
		fdDataModel = new FDDataModel(fixedDeposits);
		csDataModel = new CSDataModel(casas);
		fdcsDataModel = new FDCSDataModel(fixedDepositCasas);

	}
	
	public void searchByCustomerRef(){
		fixedDeposits = mdbServices.getListOfFixedDepositsByCustomerRef(customerId);
		fdDataModel = new FDDataModel(fixedDeposits);
		casas = mdbServices.getListOfCasaByCustomerRef(customerId);
		csDataModel = new CSDataModel(casas);
		fixedDepositCasas = mdbServices.getListOfFDCasaByCustomerRef(customerId);
		fdcsDataModel = new FDCSDataModel(fixedDepositCasas);
		
		setTotalScradAvailable(mdbServices.getSCardFDAvailable(customerId)+mdbServices.getSCardCSAvailable(customerId)+mdbServices.getSCardFDCSAvailable(customerId));
		setTotalAmountAvailable(mdbServices.getAmountCorpAvailable(customerId));
		
	}
	
	 public void onRowSelect(SelectEvent event) {  
		 luckyDrawCodes = mdbServices.getAllLuckyDrawCodeP(depositsSelected.getSavingbookNo(), 0);
	    }  
	 
	 public void onRowSelectCS(SelectEvent event) {  
		 luckyDrawCodesCS = mdbServices.getAllLuckyDrawCodeP(casaSelected.getAccountLocked(), 0);
	    } 
	  
	 public void onRowSelectFDCS(SelectEvent event) {  
		 luckyDrawCodesFDCS = mdbServices.getAllLuckyDrawCodeP(depositCasaSelected.getSavingbookNo(), 0);
	    } 
	    public void onRowUnselect(UnselectEvent event) {  
	    	FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Nhập số sổ tiết kiệm không kỳ hạn là thông tin bắt buộc!",
							"Vui lòng nhập lại"));
	    }  
	
	public void setMdbServices(MDBServices mdbServices) {
		this.mdbServices = mdbServices;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public List<FixedDeposits> getFixedDeposits() {
		return fixedDeposits;
	}
	public void setFixedDeposits(List<FixedDeposits> fixedDeposits) {
		this.fixedDeposits = fixedDeposits;
	}
	public List<FixedDepositCasa> getFixedDepositCasas() {
		return fixedDepositCasas;
	}
	public void setFixedDepositCasas(List<FixedDepositCasa> fixedDepositCasas) {
		this.fixedDepositCasas = fixedDepositCasas;
	}
	public List<Casa> getCasas() {
		return casas;
	}
	public void setCasas(List<Casa> casas) {
		this.casas = casas;
	}

	public FixedDeposits getDepositsSelected() {
		return depositsSelected;
	}

	public void setDepositsSelected(FixedDeposits depositsSelected) {
		this.depositsSelected = depositsSelected;
	}

	public FDDataModel getFdDataModel() {
		return fdDataModel;
	}

	public List<LuckyDraw> getLuckyDraws() {
		return luckyDraws;
	}

	public void setLuckyDraws(List<LuckyDraw> luckyDraws) {
		this.luckyDraws = luckyDraws;
	}

	public String getLuckyDrawCodes() {
		return luckyDrawCodes;
	}

	public void setLuckyDrawCodes(String luckyDrawCodes) {
		this.luckyDrawCodes = luckyDrawCodes;
	}

	public CSDataModel getCsDataModel() {
		return csDataModel;
	}

	public String getLuckyDrawCodesCS() {
		return luckyDrawCodesCS;
	}

	public void setLuckyDrawCodesCS(String luckyDrawCodesCS) {
		this.luckyDrawCodesCS = luckyDrawCodesCS;
	}

	public Casa getCasaSelected() {
		return casaSelected;
	}

	public void setCasaSelected(Casa casaSelected) {
		this.casaSelected = casaSelected;
	}

	public String getLuckyDrawCodesFDCS() {
		return luckyDrawCodesFDCS;
	}

	public void setLuckyDrawCodesFDCS(String luckyDrawCodesFDCS) {
		this.luckyDrawCodesFDCS = luckyDrawCodesFDCS;
	}

	public FixedDepositCasa getDepositCasaSelected() {
		return depositCasaSelected;
	}

	public void setDepositCasaSelected(FixedDepositCasa depositCasaSelected) {
		this.depositCasaSelected = depositCasaSelected;
	}

	public FDCSDataModel getFdcsDataModel() {
		return fdcsDataModel;
	}

	public int getTotalScradAvailable() {
		return totalScradAvailable;
	}

	public void setTotalScradAvailable(int totalScradAvailable) {
		this.totalScradAvailable = totalScradAvailable;
	}

	public double getTotalAmountAvailable() {
		return totalAmountAvailable;
	}

	public void setTotalAmountAvailable(double totalAmountAvailable) {
		this.totalAmountAvailable = totalAmountAvailable;
	}


}
